home *** CD-ROM | disk | FTP | other *** search
- /* StatusWindow.c */
-
- #include <Errors.h>
- #include <Script.h>
- #include <Types.h>
- #include <Files.h>
- #include <Resources.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <Windows.h>
- #include <ToolUtils.h>
- #include <Memory.h>
- #include <Menus.h>
- #include <Lists.h>
- #include <Printing.h>
- #include <Dialogs.h>
- #include <StandardFile.h>
-
- #ifndef TESTING /* Defined in the TestStatusWindow project prefix dialog */
- #define TESTING 0
- #endif
-
- #ifndef FALSE
- #define FALSE 0
- #define TRUE 1
- #endif
-
- WindowPtr MakeStatusWindow(void);
- void UpdateStatusWindow(
- WindowPtr statusWindowPtr
- );
- void ShowStatusString(
- WindowPtr statusWindowPtr,
- ConstStr255Param theText
- );
-
- /*
- * Very simple logging window. The configuration data are burned in, but could
- * easily be passed as parameters.
- */
- #define COLS 80
- #define FONT_NUMBER (applFont)
- #define FONT_SIZE (9)
- #define width(r) ((r).right - (r).left)
- #define height(r) ((r).bottom - (r).top)
-
- typedef struct StatusWindowRecord {
- WindowRecord windowRecord; /* The status window */
- Rect topLineRect; /* Line[0] display rect */
- Rect lineRect; /* Current line display rect */
- unsigned short lastUnderline; /* Previous line @ leading */
- unsigned short currentLine; /* Current line number */
- unsigned short nLines; /* Number of lines in window */
- unsigned short lineHeight; /* ascent + descent + leading */
- unsigned short lineAscent; /* Just the font ascent */
- unsigned short underlineOffset; /* Where to draw the underline */
- Ptr textArea; /* All text goes here */
- StringPtr textStrings[1]; /* -> each text line */
- } StatusWindowRecord, *StatusWindowRecordPtr;
- #define STATUS (*((StatusWindowRecordPtr) statusWindowPtr))
- #define WINDOW (*((WindowPtr) &STATUS.windowRecord))
-
- WindowPtr
- MakeStatusWindow(void)
- {
- Rect windowRect;
- StatusWindowRecordPtr statusWindowPtr;
- WindowPtr theWindow;
- FontInfo info;
- short lineHeight;
- short nLines;
- GrafPtr savePort;
- GrafPort tempPort;
- Size statusRecordSize;
- short i;
-
- GetPort(&savePort);
- /*
- * Create a temporary port so we can get the line height before
- * creating the window... and without messing up the current port.
- */
- OpenPort(&tempPort); /* Does a SetPort */
- TextFont(FONT_NUMBER);
- TextSize(FONT_SIZE);
- GetFontInfo(&info);
- ClosePort(&tempPort);
- SetPort(savePort);
- lineHeight = info.ascent + info.descent + info.leading;
- /*
- * The window occupies the right-half of the display. This, too, could
- * be passed in from the caller.
- */
- windowRect = qd.screenBits.bounds;
- InsetRect(&windowRect, 4, 4);
- windowRect.top += (GetMBarHeight() * 2);
- windowRect.left = windowRect.right - (width(windowRect) / 2);
- nLines = (height(windowRect) - 2) / lineHeight;
- windowRect.top = windowRect.bottom - ((nLines * lineHeight) + 2);
- /* */
- statusRecordSize = sizeof (StatusWindowRecord) /* Constant info */
- - sizeof (StringPtr) /* Remove C array hack */
- + (nLines * sizeof (StringPtr)); /* Add in string vector */
- statusWindowPtr = (StatusWindowRecordPtr)
- NewPtrClear(statusRecordSize);
- if (statusWindowPtr != NULL) {
- STATUS.textArea = NewPtr(nLines * (COLS + 1)); /* String data area */
- if (STATUS.textArea == NULL) {
- DisposePtr((Ptr) statusWindowPtr);
- statusWindowPtr = NULL;
- }
- }
- if (statusWindowPtr != NULL) {
- theWindow = NewCWindow(
- &STATUS.windowRecord, /* Our storage */
- &windowRect, /* Dummy window shape */
- "\pLog Display", /* Window title */
- FALSE, /* Invisible */
- noGrowDocProc, /* Minimal window */
- (WindowPtr) -1L, /* In front */
- TRUE, /* Has close box */
- 0 /* RefCon */
- );
- if (theWindow == NULL) {
- DisposePtr(STATUS.textArea);
- DisposePtr((Ptr) statusWindowPtr);
- statusWindowPtr = NULL;
- }
- }
- if (statusWindowPtr != NULL) {
- SetPort(theWindow);
- TextFont(FONT_NUMBER);
- TextSize(FONT_SIZE);
- GetFontInfo(&info);
- STATUS.currentLine = 0;
- STATUS.lastUnderline = 0;
- STATUS.lineAscent = info.ascent;
- STATUS.lineHeight = info.ascent + info.descent + info.leading;
- STATUS.underlineOffset = info.ascent + info.descent;
- STATUS.nLines = nLines;
- STATUS.lineRect = WINDOW.portRect;
- InsetRect(&STATUS.lineRect, 1, 1);
- STATUS.lineRect.bottom = STATUS.lineRect.top + STATUS.lineHeight;
- STATUS.topLineRect = STATUS.lineRect;
- /*
- * Initialize the vector of string pointers to point to the string area.
- */
- for (i = 0; i < nLines; i++) {
- STATUS.textStrings[i] = (StringPtr) &STATUS.textArea[i * (COLS + 1)];
- STATUS.textStrings[i][0] = 0; /* Initially "\p" */
- }
- ShowWindow(theWindow);
- }
- SetPort(savePort);
- return ((WindowPtr) statusWindowPtr);
- }
-
- void
- UpdateStatusWindow(
- WindowPtr statusWindowPtr
- )
- {
- short i;
- GrafPtr savePort;
- Point viewPoint;
-
- GetPort(&savePort);
- SetPort(statusWindowPtr);
- TextFont(FONT_NUMBER);
- TextSize(FONT_SIZE);
- viewPoint.h = STATUS.topLineRect.left;
- viewPoint.v = (STATUS.topLineRect.top + STATUS.lineAscent);
- for (i = 0; i < STATUS.nLines; i++) {
- if (STATUS.textStrings[i][0] != 0) {
- MoveTo(viewPoint.h, viewPoint.v);
- DrawString(STATUS.textStrings[i]);
- }
- viewPoint.v += STATUS.lineHeight;
- }
- if (STATUS.lastUnderline != 0) {
- MoveTo(WINDOW.portRect.left, STATUS.lastUnderline);
- LineTo(WINDOW.portRect.right, STATUS.lastUnderline);
- }
- SetPort(savePort);
- }
-
- void
- ShowStatusString(
- WindowPtr statusWindowPtr,
- ConstStr255Param theText
- )
- {
- short textSize;
- GrafPtr savePort;
- StringPtr thisLine;
- Rect viewRect;
-
- GetPort(&savePort);
- SetPort(statusWindowPtr);
- textSize = theText[0];
- if (textSize > COLS)
- textSize = COLS;
- thisLine = STATUS.textStrings[STATUS.currentLine];
- BlockMove(&theText[1], &thisLine[1], textSize);
- thisLine[0] = textSize;
- if (theText[0] > COLS)
- thisLine[COLS - 1] = '…';
- if (STATUS.lastUnderline != 0) {
- SetRect(
- &viewRect,
- WINDOW.portRect.left,
- STATUS.lastUnderline,
- WINDOW.portRect.right,
- STATUS.lastUnderline + 1
- );
- EraseRect(&viewRect);
- }
- EraseRect(&STATUS.lineRect);
- MoveTo(
- STATUS.lineRect.left,
- STATUS.lineRect.top + STATUS.lineAscent
- );
- DrawString(thisLine);
- STATUS.lastUnderline = STATUS.lineRect.top + STATUS.underlineOffset;
- MoveTo(STATUS.lineRect.left, STATUS.lastUnderline);
- LineTo(STATUS.lineRect.right, STATUS.lastUnderline);
- if (++STATUS.currentLine < STATUS.nLines)
- OffsetRect(&STATUS.lineRect, 0, STATUS.lineHeight);
- else {
- STATUS.currentLine = 0;
- STATUS.lineRect = STATUS.topLineRect;
- }
- SetPort(savePort);
- }
-
- #if TESTING
-
- WindowPtr gStatusWindow;
-
- void
- main(void)
- {
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0);
- /* */
- gStatusWindow = MakeStatusWindow();
- if (gStatusWindow == NULL)
- DebugStr("\pDisplay window failed");
- else {
- int i;
-
- for (i = 0; i < 40; i++)
- ShowStatusString(gStatusWindow, "\pHello world!");
- while (Button() == FALSE)
- ;
- while (Button() != FALSE)
- ;
- }
- }
- #endif
-